package com.example.umengdemo.utils

import android.annotation.SuppressLint
import android.content.Context
import android.util.Log
import com.example.umengdemo.utils.Constant.Companion.CURRENT_TIME
import com.example.umengdemo.utils.Constant.Companion.OPEN_APP
import com.example.umengdemo.utils.Constant.Companion.PHONE_MODEL
import com.example.umengdemo.utils.Constant.Companion.PHONE_VERSION
import com.example.umengdemo.utils.Constant.Companion.USERID
import com.example.umengdemo.utils.Constant.Companion.VERSION_CODE
import com.umeng.analytics.MobclickAgent
import java.util.*

/**
 * @auth: njb
 * @date: 2021/10/14 14:03
 * @desc: 友盟事件统计工具类
 */
object UmengEventUtil {
    private val TAG = UmengEventUtil::class.java
    /**
     * 用户打开App事件
     *
     * @param context
     */
    fun toOpenApp(context: Context?) {
        val map = HashMap<String, String>()
        setBaseMap(context,map)
        MobclickAgent.onEvent(context, OPEN_APP, map)
        Log.d(TAG.toString(),map.toString())
    }

    /**
     * 用户安装app事件
     *
     * @param ctx
     * @return
     */
    @SuppressLint("MissingPermission")
    fun getInstallMap(): HashMap<String, String> {
        val map = HashMap<String, String>()
        map[PHONE_MODEL] = CommonUtils.phoneModel
        map[PHONE_VERSION] = CommonUtils.versionRelease
        map[CURRENT_TIME] =
            TimeUtils.parserTimeToYmdHmSpace(System.currentTimeMillis())
        return map
    }

    /**
     * 设置基本参数
     *
     * @param context
     * @param map
     * @return
     */
    private fun setBaseMap(
        context: Context?,
        map: HashMap<String, String>
    ): HashMap<String, String> {
        val userId = "user_id"
        if (userId != null) {
            map[USERID] = userId //userId
        }
        val versionCode = context?.let { CommonUtils.getVersionName(it) }
        if (versionCode != null) {
            map[VERSION_CODE] = versionCode //版本号
        }
        val currentTime = System.currentTimeMillis()
        //当前时间
        map[CURRENT_TIME] =
            TimeUtils.parserTimeToYmdHmSpace(currentTime)
        //手机型号
        map[PHONE_MODEL] = CommonUtils.phoneModel
        return map
    }
}